import java.awt.Point;
import java.awt.Rectangle;

public class PLShapeUtils {

	public static PLShape MakeNewShape(PLShape.ShapeIdT id) {
		PLShape shape = null;
		if (id == PLShape.ShapeIdT.kLine) {
			shape = new PLLine(); 	//Create the shape - Line
		} else if (id == PLShape.ShapeIdT.kRect) {
			shape = new PLRect(); 	//Create the shape - Line
		} else if (id == PLShape.ShapeIdT.kEllipse) {
			shape = new PLEllipse();	 //Create the shape - Line
		} else if (id == PLShape.ShapeIdT.kTriangle) {
			shape = new PLIsTriangle();  //Create the shape - Line
		}		
		return shape;
	}	

	public static PLShape MakeNewShape() {
		return MakeNewShape(PLGuiInfo.Obj().GetCurShape());
	}	


	public static PLShape MakeNewShape(PLShape.ShapeIdT id, Point startpos, Point endpos) {
		PLDebug.Info("New Shape %s: (%d, %d) - (%d, %d)", id, startpos.x, startpos.y, endpos.x, endpos.y); 
		PLShape shape = null;

		if (id == PLShape.ShapeIdT.kLine) {
			shape = new PLLine(startpos, endpos); 	//Create the shape - Line
		} else if (id == PLShape.ShapeIdT.kRect) {
			shape = new PLRect(startpos, endpos); 	//Create the shape - Line
		} else if (id == PLShape.ShapeIdT.kEllipse) {
			shape = new PLEllipse(startpos, endpos);	 //Create the shape - Line
		} else if (id == PLShape.ShapeIdT.kTriangle) {
			shape = new PLIsTriangle(startpos, endpos);  //Create the shape - Line
		}		
		return shape;
	}	

	public static PLShape MakeNewShape(Point startpos, Point endpos) {
		PLDebug.Info("Making new shape for points: (%d, %d) - (%d, %d)", startpos.x, startpos.y, endpos.x, endpos.y); 

		return MakeNewShape(PLGuiInfo.Obj().GetCurShape(), startpos, endpos);
	}	

	// keeps the point inside the canvas
	public static void SanitizePoint(Point point) {
		point.x = Math.max(point.x, 0);
		point.x = Math.min(point.x, PLGuiInfo.Obj().GetWidth());

		point.y = Math.max(point.y, 0);
		point.y = Math.min(point.y, PLGuiInfo.Obj().GetHeight());
	}

	// given a starting  point, angle of the line wrt x axis, and the length, 
	// returns 2 points that can be the possible end points for the line of given length
	public static Point[] GetPossibleEndPoints(Point start, float slopeAngle, float lenght) {
		Point[] points = new Point[2];
				
		float xDist = (float)(lenght * Math.cos(slopeAngle));
		float yDist = (float)(lenght * Math.sin(slopeAngle));
		
		points[0] = new Point( (int)(start.x + xDist), (int)(start.y + yDist)); 
		points[1] = new Point( (int)(start.x - xDist), (int)(start.y - yDist)); 

		return points;
	}

	public static float GetDistance(Point start, Point end) {
		return (float)Math.sqrt((start.x - end.x)*(start.x - end.x) + (start.y - end.y)*(start.y - end.y)); 
	}

	//quadrants: 
	//0-1
	//3-2
	static final int w = PLGuiInfo.Obj().GetWidth(); 
	static final int h = PLGuiInfo.Obj().GetHeight(); 
	
	static final Rectangle rect1 = new Rectangle(0,0,w/2,h/2); 
	static final Rectangle rect2 = new Rectangle(w/2,0,w/2,h/2); 
	static final Rectangle rect3 = new Rectangle(w/2,h/2,w/2,h/2); 
	static final Rectangle rect4 = new Rectangle(0,h/2, w/2, h/2); 

	public static Rectangle GetQuadrantRect(int quad) {
		switch (quad) {
			case 0: return rect1;
			case 1: return rect2;
			case 2: return rect3;
			case 3: return rect4;
		}
		return null;
	}
	
	public static int GetQuadrant(Point point) {
			if (rect1.contains(point))
				return 0;

			if (rect2.contains(point))
				return 1;

			if (rect3.contains(point))
				return 2;

			if (rect4.contains(point))
				return 3;
			
			return -1;
	} 		

	
	
}
